package br.com.rglariguet.rgportal.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.rglariguet.rgportal.Cliente;
import br.com.rglariguet.rgportal.dao.ClienteDAO;

@Repository
@Transactional
public class ClienteDAOImpl implements ClienteDAO {

	private final SessionFactory factory;
	
	@Autowired
	public ClienteDAOImpl (SessionFactory factory){
		this.factory = factory;
	}
	
	private Session getSession() {
		return factory.getCurrentSession();
	}

	@Override
	public Cliente salvar(Cliente cliente) {         				
		getSession().save(cliente);  	
		return cliente;
	}
	
	@Override
	public List<Cliente> listarTodos() {
		return (List<Cliente>)getSession().createQuery("from Cliente as cli order by cli.nomeFantasia").list();
	}

	@Override
	public Cliente buscarPorId(Long id) {
		return (Cliente) getSession().get(Cliente.class, id);
	}
	
	@Override
	public void alterar(Cliente cliente) {
		getSession().update(cliente);
	}

	@Override
	public void excluir(Cliente cliente) {
		getSession().delete(cliente);
	}
	
}
